/**
 * Copyright (c) 2011-2012, James Zhan 詹波 (jfinal@126.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.wholesale.c3p0;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.wholesale.kit.StringKit;
import com.wholesale.plugin.IPlugin;
import com.wholesale.plugin.activerecord.IDataSourceProvider;

/**
 * The c3p0 datasource plugin.
 */
public class C3p0Plugin implements IPlugin, IDataSourceProvider {

    private String jdbcUrl;
    private String user;
    private String password;
    private String driverClass = "com.mysql.jdbc.Driver";
    private int maxPoolSize = 100;
    private int minPoolSize = 10;
    private int initialPoolSize = 10;
    private int maxIdleTime = 20;
    private int acquireIncrement = 2;

    private ComboPooledDataSource dataSource;

    public C3p0Plugin setDriverClass(String driverClass) {
        if (StringKit.isBlank(driverClass))
            throw new IllegalArgumentException("driverClass can not be blank.");
        this.driverClass = driverClass;
        return this;
    }

    public C3p0Plugin(String jdbcUrl, String user, String password) {
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;
    }

    public C3p0Plugin(String jdbcUrl, String user, String password, String driverClass) {
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;
        this.driverClass = driverClass != null ? driverClass : this.driverClass;
    }

    public C3p0Plugin(String jdbcUrl,
                      String user,
                      String password,
                      String driverClass,
                      Integer maxPoolSize,
                      Integer minPoolSize,
                      Integer initialPoolSize,
                      Integer maxIdleTime,
                      Integer acquireIncrement) {
        initC3p0Properties(jdbcUrl,
                           user,
                           password,
                           driverClass,
                           maxPoolSize,
                           minPoolSize,
                           initialPoolSize,
                           maxIdleTime,
                           acquireIncrement);
    }

    private void initC3p0Properties(String jdbcUrl,
                                    String user,
                                    String password,
                                    String driverClass,
                                    Integer maxPoolSize,
                                    Integer minPoolSize,
                                    Integer initialPoolSize,
                                    Integer maxIdleTime,
                                    Integer acquireIncrement) {
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;
        this.driverClass = driverClass != null ? driverClass : this.driverClass;
        this.maxPoolSize = maxPoolSize != null ? maxPoolSize : this.maxPoolSize;
        this.minPoolSize = minPoolSize != null ? minPoolSize : this.minPoolSize;
        this.initialPoolSize = initialPoolSize != null ? initialPoolSize : this.initialPoolSize;
        this.maxIdleTime = maxIdleTime != null ? maxIdleTime : this.maxIdleTime;
        this.acquireIncrement = acquireIncrement != null ? acquireIncrement : this.acquireIncrement;
    }

    public C3p0Plugin(File propertyfile) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(propertyfile);
            Properties ps = new Properties();
            ps.load(fis);

            initC3p0Properties(ps.getProperty("jdbcUrl"),
                               ps.getProperty("user"),
                               ps.getProperty("password"),
                               ps.getProperty("driverClass"),
                               toInt(ps.getProperty("maxPoolSize")),
                               toInt(ps.getProperty("minPoolSize")),
                               toInt(ps.getProperty("initialPoolSize")),
                               toInt(ps.getProperty("maxIdleTime")),
                               toInt(ps.getProperty("acquireIncrement")));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (fis != null)
                try {
                    fis.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    public C3p0Plugin(Properties properties) {
        Properties ps = properties;
        initC3p0Properties(ps.getProperty("jdbcUrl"),
                           ps.getProperty("user"),
                           ps.getProperty("password"),
                           ps.getProperty("driverClass"),
                           toInt(ps.getProperty("maxPoolSize")),
                           toInt(ps.getProperty("minPoolSize")),
                           toInt(ps.getProperty("initialPoolSize")),
                           toInt(ps.getProperty("maxIdleTime")),
                           toInt(ps.getProperty("acquireIncrement")));
    }

    public boolean start() {
        dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        try {
            dataSource.setDriverClass(driverClass);
        }
        catch (PropertyVetoException e) {
            dataSource = null;
            System.err.println("C3p0Plugin start error");
            throw new RuntimeException(e);
        }
        dataSource.setMaxPoolSize(maxPoolSize);
        dataSource.setMinPoolSize(minPoolSize);
        dataSource.setInitialPoolSize(initialPoolSize);
        dataSource.setMaxIdleTime(maxIdleTime);
        dataSource.setAcquireIncrement(acquireIncrement);

        return true;
    }

    private Integer toInt(String str) {
        return Integer.parseInt(str);
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public boolean stop() {
        if (dataSource != null)
            dataSource.close();
        return true;
    }
}
